package com.example.demo.thread.atomic;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Description:
 * @Author: LLL
 * @Date: 2020-03-18 18:54
 **/
public class ContainerNotSafeDemo {
    public static void main(String[] args) throws InterruptedException {
        List<String> unmodifiableList = new ArrayList<>();
        // List<String> list = new Vector<>();
        // List<String> unmodifiableList = Collections.synchronizedList(list);
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(() -> {
                unmodifiableList.add(UUID.randomUUID().toString().substring(0, 8));
            });
            thread.start();
            // 等待当前线程执行结束 再执行下一次循环 主线程才可以继续执行
            // 当我们调用某个线程的这个方法时，这个方法会挂起调用线程，直到被调用线程结束执行，调用线程才会继续执行
            thread.join();
            // try {
            // TimeUnit.SECONDS.sleep(1);
            // } catch (InterruptedException e) {
            // e.printStackTrace();
            // }
        }
        /*
         * try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }
         */
        System.out.println(unmodifiableList);
        // Concurrent
    }
}
